package com.example.controller.buyer;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.dto.CouponDiscountDTO;
import com.example.dto.OrderItemDTO;
import com.example.service.CouponService;
import com.example.service.DiscountService;
import com.example.service.OrderService;
import com.example.service.UserCouponService;
import com.example.vo.*;
import com.example.vo.buyer.CartCouponVo;
import com.example.vo.buyer.DiscountDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author circle48
 */
@RestController
@Slf4j
@Api(tags = "买家端,买家优惠券接口")
@RequestMapping("/buyer/promotion/coupon")
public class CouponBuyerController {

    /**
     * 优惠券
     */
    @Resource
    private CouponService couponService;


    /**
     * 会员优惠券
     */
    @Resource
    private UserCouponService userCouponService;

    /**
     * 折扣计算
     */
    @Resource
    private DiscountService discountService;


    @Resource
    private OrderService orderService;

    @ApiOperation(value = "获取对应店铺的优惠券列表")
    @GetMapping("/getCouponsByStore")
    public Result<CartCouponVo> getCouponsByStore(@RequestParam Long storeId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long id = loginUser.getUser().getId();
        return userCouponService.getCouponsByStore(id,storeId);
    }

    @ApiOperation(value = "获取当前会员的优惠券列表")
    @GetMapping("/getCoupons")
    public Result<IPage<UserCouponVO>> getCoupons(Integer pageNum,
                                                  Integer pageSize,
                                                  @RequestParam(required = false) String couponStatus) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long id = loginUser.getUser().getId();
//        long id=1;
        return userCouponService.getMemberCoupons(pageNum,pageSize,couponStatus,id);
    }

    @GetMapping
    @ApiOperation(value = "获取可领取优惠券列表")
    public Result<IPage<CouponVO>> getCouponList(Integer pageNum,
                                                 Integer pageSize) {

        return couponService.getCouponList(pageNum,pageSize);
    }

    @ApiOperation(value = "会员领取优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券ID", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/receive/{couponId}")
    public Result receiveCoupon(@NotNull(message = "优惠券ID不能为空") @PathVariable("couponId") Long couponId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long id = loginUser.getUser().getId();
        return userCouponService.receiveBuyerCoupon(couponId, id);
    }

    @ApiOperation(value = "获取当前会员的对于当前商品可使用的优惠券列表")
    @GetMapping("/canUse")
    public Result<List<CouponDiscountDTO>> getCouponsByCanUse() {
        Long userId = IdHolder.getUserId();
        List<OrderItemDTO> orderList = orderService.getOrderList(userId);
        return Result.ok(discountService.findDiscountSolution(orderList));

    }
//    @ApiOperation(value = "获取当前会员的对于当前商品可使用的优惠券列表")
//    @GetMapping("/canUse")
//    public Result<List<CouponDiscountDTO>> getCouponsByCanUse(@RequestBody List<OrderItemDTO> orderList) {
//        System.out.println(orderList);
//        System.out.println(discountService.findDiscountSolution(orderList));
//        return Result.ok(discountService.findDiscountSolution(orderList));
//
//    }


    @ApiOperation(value = "将选中方案的数据存入redis")
    @PostMapping("/discount")
    public Result<?> saveDiscountDetails(@RequestBody List<DiscountDetailVo> discountDetails){
        Long userId = IdHolder.getUserId();
        boolean isUpdata = couponService.saveDiscountDetails(userId, discountDetails);
        if (isUpdata){
            return Result.ok();
        }
        return Result.error("未获取方案");
    }

    @ApiOperation(value = "刷新页面将优惠方案详情清空")
    @PostMapping("/clear")
    public Result<?> clearDiscountDetails(){
        Long userId = IdHolder.getUserId();
        boolean isUpdata = couponService.clearDiscountDetails(userId);
        if (isUpdata){
            return Result.ok();
        }
        return Result.error("操作失败");
    }

    @ApiOperation(value = "通过id获取优惠券详情")
    @GetMapping("/getById")
    public Result<UserCouponVO> getCouponsById(@RequestParam("couponID") Long couponId) {
        return Result.ok(userCouponService.getCouponsById(couponId));
    }


    @ApiOperation(value = "批量获取优惠券")
    @GetMapping("/getByIds")
    public Result<List<UserCouponVO>> getCouponsByIds(@RequestParam("couponIDs") List<Long> couponIds) {
        return Result.ok(userCouponService.getCouponsByIds(couponIds));
    }
}
